package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.exception.LogicException;
import cn.wolfcode.wolf2w.redis.service.IRedisService;
import cn.wolfcode.wolf2w.redis.test.RedisByKey;
import cn.wolfcode.wolf2w.repository.UserInfoRepository;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.AssertUtil;
import cn.wolfcode.wolf2w.util.Consts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.EmptyStackException;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Service
public class UserInfoRepositoryImpl implements IUserInfoService {

    @Autowired
    private UserInfoRepository userInfoRepository;
    @Autowired
    private IRedisService redisService;

    @Override
    public void insert(UserInfo userInfo) {
        userInfoRepository.insert(userInfo);
    }

    @Override
    public void delete(String id) {
        userInfoRepository.deleteById(id);
    }

    @Override
    public void update(UserInfo userInfo) {
        userInfoRepository.save(userInfo);
    }

    @Override
    public UserInfo selectOne(String id) {
        UserInfo userInfo = userInfoRepository.findById(id).orElse(null);
        //UserInfo userInfo = byId.get();
        return userInfo;
    }

    @Override
    public List<UserInfo> selectAll() {
        List<UserInfo> all = userInfoRepository.findAll();
        return all;
    }

    @Override
    public boolean checkPhone(String phone) {
        //通过电话号码为查询条件，看数据中是否有该用户
        UserInfo userInfo=userInfoRepository.findByPhone(phone);
        return userInfo!=null;
    }

    @Override
    public String getCode(String phone) {
        RestTemplate restTemplate = new RestTemplate();
        String code1 = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 4);
        //模拟用信息发送验证码
        //StringBuilder sb=new StringBuilder();
        //sb.append("请您在").append(Consts.VERIFY_CODE_VAI_TIME).append("分钟之内使用").append(code1).append("验证码");
        String join = RedisByKey.VERIFY_CODE.join(code1);
        //String template = restTemplate.getForObject("https://way.jd.com/chuangxin/dxjk?mobile={1}&content=【创信】你的验证码是:{2}，3分钟内有效！&appkey={3}", String.class, phone, join, "1dd2147188b514720018d0e2cdc565e2");
        //if(!template.contains("Success")){
        //    throw new LogicException("发送失败");
        //}
        System.out.println(join);
        return code1;
    }

    @Override
    public void regist(String phone, String nickname, String password, String rpassword, String verifyCode) {
        //校验参数是否为空
        AssertUtil.isEmpty(phone, "号码不能为空");
        AssertUtil.isEmpty(nickname, "昵称不能为空");
        AssertUtil.isEmpty(password, "密码不能为空");
        AssertUtil.isEmpty(rpassword, "重输密码不能为空");
        AssertUtil.isEmpty(verifyCode, "验证码不能为空");
        //校验2次输入的密码是否相等
        AssertUtil.isEquals(password, rpassword, "您再次输入的密码不正确");
        //校验手机号是否正确
        //校验手机号是否唯一
        if(this.checkPhone(phone)){
            throw new LogicException("您的手机号不唯一");
        }
        //校验短信验证验证码是否正确
        String code = redisService.getRedis(phone);
        if(!verifyCode.equalsIgnoreCase(code)){
            throw new LogicException("您输入的验证码不正确");
        }
        //进行注册
        UserInfo userInfo = new UserInfo();
        userInfo.setNickname(nickname);
        userInfo.setPhone(phone);
        userInfo.setEmail("@1111");
        userInfo.setGender(0);
        userInfo.setPassword(password);
        userInfo.setLevel(0);
        userInfo.setHeadImgUrl("images/default.jpg");
        this.insert(userInfo);
    }

    @Override
    public UserInfo login(String username, String password) {
        UserInfo byPhone = userInfoRepository.findByPhone(username);
        return byPhone;
    }

    //用户通过所在城市进行查找
    @Override
    public List<UserInfo> queryByCity(String name) {
        List<UserInfo> userInfos=userInfoRepository.queryByCity(name,null);
        return userInfos;
    }
}
